在if else语句中传递id属性

时间:2014-11-26 04:10:21

标签: javascript html

我想做的是if else调用html中的id。我显示了一小段代码,但结果并没有根据给定的条件出来。它仅显示最后一个警报(' *新密码不匹配')。

关于此,我有谷歌。我可以找到的关闭是Using the id for if condition,但这只能通过在' div'中传递id来完成。元件。

还有其他方法可以做一些与我的代码非常相似的事情吗?我知道我的语法不是100%正确,这就是我需要专家建议的原因。感谢您提前的时间

以下是if else语句

   if( $('new_pwd').attr('id')  === "") {
            alert('*New Password Empty');
    } else if ( $('retype_pwd').attr('id') === "") {
            alert('*Retype Password Empty');
    } else if( $('new_pwd').attr('id') != $('retype_pwd').attr('id')) {
            alert('*New Passwords do not match') ;
    };      

HTML

<table>
   <tr>
      <td>New Password</td>
      <td><input id="new_pwd" type="password" name="new_password"></input></td>
   </tr>
   <tr>
      <td>Retype Password</td>
      <td><input id="retype_pwd" type="password" name="retype_password"></input></td>
   </tr>
</table>

3 个答案:

答案 0 :(得分:1)

您必须使用#在选择器中调出ID,例如$(&#39; #new_pwd&#39;)。基本上,jquery选择器的工作方式类似于CSS,您使用#来引用元素的id,.用于类,纯文本将尝试匹配DOM元素,如$(&#39; table tr td&#39;)。

有关完整文档,请参阅http://api.jquery.com/id-selector/

编辑:您还必须使用val()来获取输入字段的值。对于您的代码,以下内容应该有效:

if( $('#new_pwd').val() === "") {
        alert('*New Password Empty');
} else if ( $('#retype_pwd').val() === "") {
        alert('*Retype Password Empty');
} else if( $('#new_pwd').val() !== $('#retype_pwd').val()) {
        alert('*New Passwords do not match') ;
};      

有关val的更多信息:http://api.jquery.com/val/

答案 1 :(得分:0)

我不完全确定你要做什么,但是也要通过它的id选择一个元素,你需要一个哈希符号。因此$('#retype_pwd')选择id为'retype_pwd'的元素。

通过离开哈希标志,您试图选择那种元素。即$('div')选择所有<div> s。

答案 2 :(得分:0)

首先,你需要#为div选择器,并且你正在计算你的div的id,你应该比较输入的值。

DEMO

<强> jQuery的:

$('#click').click(function() {
if( $('#new_pwd').val()  === "") {
            alert('*New Password Empty');
    } else if ( $('#retype_pwd').val() === "") {
            alert('*Retype Password Empty');
    } else if( $('#new_pwd').val() != $('#retype_pwd').val()) {
            alert('*New Passwords do not match') ;
    };      
});

<强> HTML:

<table>
   <tr>
      <td>New Password</td>
      <td><input id="new_pwd" type="password" name="new_password"></input></td>
   </tr>
   <tr>
      <td>Retype Password</td>
      <td><input id="retype_pwd" type="password" name="retype_password"></input></td>
   </tr>
</table>
<button id = "click" >click</button>

&#13;
&#13;
$('#click').click(function() {
if( $('#new_pwd').val()  === "") {
            alert('*New Password Empty');
    } else if ( $('#retype_pwd').val() === "") {
            alert('*Retype Password Empty');
    } else if( $('#new_pwd').val() != $('#retype_pwd').val()) {
            alert('*New Passwords do not match') ;
    };      
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<table>
   <tr>
      <td>New Password</td>
      <td><input id="new_pwd" type="password" name="new_password"></input></td>
   </tr>
   <tr>
      <td>Retype Password</td>
      <td><input id="retype_pwd" type="password" name="retype_password"></input></td>
   </tr>
</table>
<button id = "click" >click</button>
&#13;
&#13;
&#13;