繁体   English   中英

在if else语句中传递id属性

[英]Passing id attribute in if else statement

我想在其他情况下在html中调出ID。 我显示了我的代码的一小段,但结果并没有根据给定的条件得出。 它仅显示最后一个警报(“ *新密码不匹配”)。

我对此有谷歌。 我可以找到的结束是对if条件使用id ,但是只能通过在'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>

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

有关完整的文档,请参见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/

我不确定您要做什么,但是也要通过其ID选择一个元素,在此之前您需要一个哈希符号。 因此$('#retype_pwd')选择ID为'retype_pwd'的元素。

通过省略哈希符号,您正在尝试选择那种element $('div')选择所有<div>

首先,您需要使用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>

 $('#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') ; }; }); 
 <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> 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM