简体   繁体   English

设置已禁用输入的标题属性

[英]Setting title attribute for a disabled input

I have an HTML form. 我有一个HTML表单。 Before the user can submit it, they have to agree to my privacy policy. 在用户提交之前,他们必须同意我的隐私政策。

Before accepting the privacy policy, the button is disabled and look like this: <input type="submit" value="Invia" class="wpcf7-form-control wpcf7-submit" disabled="disabled"> . 在接受隐私政策之前,该按钮被禁用,如下所示: <input type="submit" value="Invia" class="wpcf7-form-control wpcf7-submit" disabled="disabled">

Question is: how do I display via jquery a different title based on the disabled property? 问题是:如何根据禁用的属性通过jquery显示不同的标题? If the button is disabled, title should be "Please agree to our privacy policy to continue". 如果该按钮被禁用,标题应为“请同意我们的隐私政策继续”。 If the button is not disabled, it should be "Send your request". 如果未禁用该按钮,则应为“发送您的请求”。

I tried different approaches, but none is working. 我尝试了不同的方法,但都没有。

My guess would have been something like: 我的猜测可能是这样的:

    /* title for disabled submit */
    if ($("input.wpcf7-submit").attr('disabled') == "disabled") {
      $("input.wpcf7-submit").attr( "title", "Please agree to our privacy policy to continue" );
    }
    else {
      $("input.wpcf7-submit").attr( "title", "Send your request" );
    }

You need to use prop to set the title attribute: 您需要使用prop来设置title属性:

if ($("input.wpcf7-submit").attr('disabled') == "disabled") {
  $("input.wpcf7-submit").prop( "title", "Please agree to our privacy policy to continue" );
}
else {
  $("input.wpcf7-submit").prop( "title", "Send your request" );
}

don 't forget to wrap it in $document.ready(function () { ... }) . 别忘了将它包装在$document.ready(function () { ... })

Basically it goes like this... 基本上就是这样......

$(functions() {
    if( $('.wpcf7-submit').is(':disabled') ){
        $('.wpcf7-submit').attr( "title", "Please agree to our privacy policy to continue" );
    }
});

You don't need an else because otherwise the default title will be displayed. 您不需要else ,否则将显示默认标题。

I believe this is what you need. 我相信这就是你所需要的。 You can run the snippet to check. 您可以运行代码段进行检查。

  $("#accCheck").change(function () { // this represents the checkbox that was checked // do something with it if ($(this).prop("checked")) { $("#btnAcc").prop("disabled", false); $("#btnAcc").prop("value", "Send your request"); } else { $("#btnAcc").prop("disabled", true); $("#btnAcc").prop("value", "Please agree to our privacy policy to continue"); } }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <input type="checkbox" id="accCheck" class="wpcf7-form-control wpcf7-submit">I accept the conditions <br/> <input type="submit" id="btnAcc" value="Invia" class="wpcf7-form-control wpcf7-submit" disabled="disabled"> 

The solution is based on the hint that CBroe gave me in a comment to this question. 解决方案是基于CBroe在评论这个问题时给我的暗示。 I write it here for everybody's convenience. 我这里写的是为了方便大家。

/* title for disabled submit button */
$("input.wpcf7-submit").attr( "title", "Please agree to our privacy policy to continue" );
$("input.Privacy").change(function() { 
 if ($(this).prop('checked') == true) {
  $("input.wpcf7-submit").attr( "title", "Send your request" ); 
 } 
 else { 
  $("input.wpcf7-submit").attr( "title", "Please agree to our privacy policy to continue" ); 
 } 
});

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

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