比较文本字段和选择列表值和警报

时间:2012-11-28 06:55:19

标签: javascript

我有一个文本字段,在屏幕加载时会填充值,并且还有一个选择列表。

我的问题是当用户在选择列表中选择一个值并与文本字段进行比较时,如果它不相等则会触发警告消息。

请帮忙!

    JS:
function doType() 
{
    if (document.getElementById("Text1").value != document.getElementById("Select1").value) 
    alert(document.getElementById("Select1").value)
}

HTML:
<tr>
        <td align="right"><b>Select 1:</b></td>
        <td align=left>
           <select name="Select1" onChange="doType();"></select>
        </td>
        <td align="right"><b> Text 1:</b></td>
        <td align="left">
            <input name="Text1" type="text" size="10" maxlength="10">
        </td>
    </tr>

1 个答案:

答案 0 :(得分:0)

JS:

function doType()
    {
      if(document.getElementById("Select1").value === document.getElementById("Text1").value){
        alert("Equal");
       }

      else{
       alert(" Not Equal");
      }
    }

HTML:

<select id="Select1"onChange="doType();">
<option>Apple</option>
<option>Orange</option>
<option>Pineapple</option>
<option>Banana</option>
</select>

<input id="Text1" type="text" value="Apple"/>