简体   繁体   English

Bootstrap 4输入外部值

[英]Bootstrap 4 input ext value

I'm trying to build a form using bootstrap 4 but as far as I'm trying to, I never succeed in setting the value in input text client side for modal form , it never works... 我正在尝试使用Bootstrap 4构建表单,但就我尝试而言,我从未成功在输入文本客户端为模式形式设置值,但从未成功...

The form is: 表格是:

 $('#contact-modal').on('show.bs.modal', function() { alert("prima"); $("#name").attr({ "value": 'ëtestí' }); }) 
 <script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script> <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous"> <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script> <button type="button" class="btn btn-primary" data-toggle="modal" data-target="#contact-modal"> modal </button> <div id="contact-modal" class="modal fade" role="dialog"> <div class="modal-dialog"> <div class="modal-content"> <div class="modal-header"> <a class="close" data-dismiss="modal">◊</a> <h3>Contact Form</h3> </div> <form id="contactForm" name="contact" role="form"> <div class="modal-body"> <div class="form-group"> <label for="name">Name</label> <input type="text" name="name" class="form-control"> </div> <div class="form-group"> <label for="email">Email</label> <input type="email" name="email" class="form-control"> </div> <div class="form-group"> <label for="message">Message</label> <textarea name="message" class="form-control"></textarea> </div> </div> <div class="modal-footer"> <button type="button" class="btn btn-default" data-dismiss="modal">Close</button> <input type="submit" class="btn btn-success" id="submit"> </div> </form> </div> </div> </div> 

The function that never works is: 永远无法使用的功能是:

  $('#contact-modal').on('show.bs.modal', function() {
    alert("prima");
    $("#name").attr({
      "value": ëtestí
    });
  })

I can really use any help, thanks! 我真的可以使用任何帮助,谢谢!

Try like that 这样尝试

<script>
$('#contact-modal').on('show.bs.modal', function() {
    alert("prima");
    $("#name").val("etesti")
})
</script>

The gist is that .attr(...) is only getting the objects value at the start (when the html is created). 要点是.attr(...)仅在开始时(创建html时)获取对象值。 val() is getting the object's property value which can change many times. val()获取对象的属性值,该值可以多次更改。

Try this: 尝试这个:

<script>
$('#contact-modal').on('show.bs.modal', function() {
    alert("prima");
    $("#name").val("etesti")
})
</script>

You should target the input by name: 您应该按名称定位输入:

<script>
    $('#contact-modal').on('show.bs.modal', function() {
        alert("prima");
        $("input[name='name']").val("ëtestí");
    })
</script>

Your issues: 您的问题:

  • The attr method uses two parameters ( "key", value ) and not an object ( {key: value} ) attr方法使用两个参数( "key", value )而不是对象( {key: value}
  • For changing the values of form elements, you should prefer the jQuery's val() method 为了更改表单元素的值,您应该首选jQuery的val()方法
  • The jQuery selector you use ( "#name" ), doesn't exist. 您使用的jQuery选择器( "#name" )不存在。 You have an input with name attribute but not with id="name" . 您的输入具有name属性,但没有id="name" So either add id="name" to the input element or change the jQuery selector to ( "[name=name] ) 因此,可以在输入元素中添加id="name"或将jQuery选择器更改为( "[name=name]
  • Your value ëtestí isn't a string and a variable with that name does not exist. 您的值“ ëtestí不是字符串,并且不存在具有该名称的变量。 I recon this is due to some copy and paste errors. 我确认这是由于某些复制和粘贴错误。
  • Your html is slightly of: The for attribute of the label element targets at the id attribute not the name attribute. 您的html有点: label元素的for属性定位于id属性而不是name属性。 This raises some accessibility issues. 这引起了一些可访问性问题。 You should either add an appropriate id attribute to the label, or wrap the label around the form element 您应该将适当的id属性添加到标签,或将标签包裹在form元素周围

I've integrated all this in this snippet 我已经将所有这些都集成到了此片段中

 $('#contact-modal').on('show.bs.modal', function() { $("[name=name]").val("ëtestí"); }) 
 <script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script> <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous"> <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script> <button type="button" class="btn btn-primary" data-toggle="modal" data-target="#contact-modal"> modal </button> <div id="contact-modal" class="modal fade" role="dialog"> <div class="modal-dialog"> <div class="modal-content"> <div class="modal-header"> <a class="close" data-dismiss="modal">◊</a> <h3>Contact Form</h3> </div> <form id="contactForm" name="contact" role="form"> <div class="modal-body"> <div class="form-group"> <label for="contactForm-name">Name</label> <input type="text" id="contactForm-name" name="name" class="form-control"> </div> <div class="form-group"> <label for="contactForm-email">Email</label> <input type="email"id="contactForm-email" name="email" class="form-control"> </div> <div class="form-group"> <label for="contactForm-message">Message</label> <textarea id="contactForm-message" name="message" class="form-control"></textarea> </div> </div> <div class="modal-footer"> <button type="button" class="btn btn-default" data-dismiss="modal">Close</button> <input type="submit" class="btn btn-success" id="submit"> </div> </form> </div> </div> </div> 

in JQuery : 在JQuery中:

use # for id $('#myid') , <input type="text" id="myid" /> 将#用作ID $('#myid')<input type="text" id="myid" />

use . 采用 。 for class $('.myclass') , <input type="text" class="myclass" /> 对于类$('.myclass')<input type="text" class="myclass" />

if id and class not given , then use other attribute 如果没有给定id和class,则使用其他属性

like for $("[name=myname]") or $("input[name=myname]") 例如$("[name=myname]")$("input[name=myname]")

<input type="text" name="myname" />

for $("input[type=text]") <input type="text" /> 对于$("input[type=text]") <input type="text" />

 $('#contact-modal').on('show.bs.modal', function() { $("input[name=name]").val('ëtestí'); }); 
 <script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script> <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous"> <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script> <button type="button" class="btn btn-primary" data-toggle="modal" data-target="#contact-modal"> modal </button> <div id="contact-modal" class="modal fade" role="dialog"> <div class="modal-dialog"> <div class="modal-content"> <div class="modal-header"> <a class="close" data-dismiss="modal">◊</a> <h3>Contact Form</h3> </div> <form id="contactForm" name="contact" role="form"> <div class="modal-body"> <div class="form-group"> <label for="name">Name</label> <input type="text" name="name" class="form-control"> </div> <div class="form-group"> <label for="email">Email</label> <input type="email" name="email" class="form-control"> </div> <div class="form-group"> <label for="message">Message</label> <textarea name="message" class="form-control"></textarea> </div> </div> <div class="modal-footer"> <button type="button" class="btn btn-default" data-dismiss="modal">Close</button> <input type="submit" class="btn btn-success" id="submit"> </div> </form> </div> </div> </div> 

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

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