简体   繁体   English

在引导模式中使用 jquery 更改表单输入字段值

[英]Change form input field value with jquery within a bootstrap modal

I have a form within a bootstrap modal.我在引导模式中有一个表单。 I would like the input fields to display the past values that the user entered the first time they filled out the form, which I am retrieving from a cloudant database.我希望输入字段显示用户在第一次填写表单时输入的过去值,我正在从云数据库中检索这些值。 I would like these input fields to be editable so that the user can resubmit the form with any changes they may have.我希望这些输入字段是可编辑的,以便用户可以重新提交他们可能有的任何更改的表单。 However, I am stuck when trying to display the past information in the input fields.但是,当我试图在输入字段中显示过去的信息时,我被卡住了。 I can't find any reason why these value of the input fields are not being changed.我找不到任何不更改输入字段的这些值的原因。

The javascript that adds my buttons to my boostrap list-group:将我的按钮添加到我的 boostrap 列表组的 javascript:

var loadIotPanel = function(iotsJSON)
{
for(var iot in iotsJSON)
    {

        var button = $('<button/>').text(iotsJSON[iot].appID).addClass('list-group-item iot').attr({name:iotsJSON[iot].appID, "aria-label": "Quick View IoT", "data-toggle": "modal", "data-target": "#quick-view-iot-modal", type: "button"});
        $('#iot-list').append(button);
    }
};

The html where my button will be placed within my list-group:我的按钮将放置在列表组中的 html:

<div class="row">
            <div class="col-lg-4 col-md-6 col-sm-12 col-sm-12 dash-col">
                <button name="edit-iots" aria-label="Edit IoTs" data-toggle="modal" data-target="#edit-iots-modal" type="button" class="icon"><span class="glyphicon glyphicon-edit gray"></span></button>
                <p class="fancy-font dash-item-title">Your IoTs</p>
                <button name="add-iot" aria-label="Add IoT" data-toggle="modal" data-target="#connect-iot-modal" type="button" class="icon"><span class="glyphicon glyphicon-plus gray"></span></button>
                <div class="list-group iot" id="iot-list"><!-- buttons will be placed here --></div>

            </div>...

The modal that pops up when clicking a button:单击按钮时弹出的模态:

<div class="modal fade" id="quick-view-iot-modal">
  <div class="modal-dialog" role="document">
<div class="modal-content">
  <div class="modal-header">
    <button type="button" class="close" data-dismiss="modal" aria-label="Close">
      <span aria-hidden="true">&times;</span>
    </button>
    <h4 class="modal-title" id="quick-view-iot-h4"></h4>
  </div>
  <div class="modal-body">
        <form id="edit-iot-form" method="post">
        IoT Name: <br>
        <input type="text" name="iot-name" class="iot-value" required><br>
        Organization ID: <br>
        <input type="text" name="org-id" class="iot-value" readonly required><br>
        Authentication Method: <br>
        <input type="text" name="auth-method" class="iot-value" value="apikey" readonly><br>
        API Key: <br>
        <input type="text" name="api-key" class="iot-value" readonly required><br>
        Authentication Token: <br>
        <input type="text" name="auth-token" class="iot-value" readonly required><br>
        </form>
        <button name="more" type="button" class="fancy-font page-button">More</button>
  </div>
</div><!-- /.modal-content -->
</div><!-- /.modal-dialog -->
</div><!-- /.modal -->

The javascript that adds the content to modal.将内容添加到模态的 javascript。 It is within a main method that is called when document is ready:它位于文档准备就绪时调用的主要方法中:

 $('.iot').click(
    function()
    {
        var iotName = event.target.name;
        getIots(loadIotQuickView, iotName);
        $('h4#quick-view-iot-h4.modal-title').text(event.target.name);


    });

The getIots() function: (it includes parameter variable=null because I also use it in another part of code where the varaible parameter is not used. This method sends an ajax post to a servlet, which then responds with an array of iot objects) getIots() function:(它包含参数 variable=null,因为我还在代码的另一部分中使用了它,其中未使用可变参数。此方法将 ajax 发送到 servlet,然后用一组物联网对象进行响应)

var getIots = function(callback, variable=null)
{
var iotsJSON = null;
$.post("DashboardServlet", {loadIotPanel: "true"}, function(response)
     {
        iotsJSON = response;

        callback(response, variable);
     });

The loadIotQuickView() function: (This is where I believe I am getting problems) loadIotQuickView() function:(这是我认为我遇到问题的地方)

var loadIotQuickView = function(iotsJSON, iotName)
{
var currentIoT = null;

for(var iot in iotsJSON)
{
    if(iotsJSON[iot].appID = iotName)
        currentIoT = iotsJSON[iot];

}
if(currentIoT == null)
    return;
$('.iot-value[name="iot-name"]').attr("value",currentIoT.appId);


};

I've tried numerous jquery selectors but none of the input field values within the form will change.我已经尝试了许多 jquery 选择器,但表单中的输入字段值都不会改变。 I've stepped through the code and all of my variables have the correct values that its just the last line that's not executing how I want.我已经逐步完成了代码,我的所有变量都具有正确的值,它只是最后一行没有按照我想要的方式执行。 I am unsure if there is an issue with my jquery selector or if it is something else.我不确定我的 jquery 选择器是否存在问题,或者是否存在其他问题。 Thanks in advance!提前致谢!

UPDATE : I've tried the following selectors:更新:我尝试了以下选择器:

$('.iot-value[name="iot-name"]')
$('input[name="iot-name"]')
$('.iot-value')
$('#connect-iot-modal').find('input[name="iot-name"]') 

I've also tried adding an id of "edit-iot-name" to my input field:我还尝试在我的输入字段中添加“edit-iot-name”的 ID:

$('#edit-iot-name')
$('#connect-iot-modal #edit-iot-name')

But none of these have worked, which leads me to believe that it must not be an issue with my selector.但是这些都没有用,这让我相信这一定不是我的选择器的问题。

UPDATE :更新

I've also tried using .val(currentIoT.appID) with all of the previous selectors.我还尝试将.val(currentIoT.appID)与之前的所有选择器一起使用。 Still not working.还是行不通。

I'm not sure why, but adding the id of the modal that my form is in, which is #quick-view-iot-modal , to my selector worked.我不知道为什么,但是将我的表单所在的模式的 id 添加到我的选择器中,即#quick-view-iot-modal However for some reason it only works when I use .val() and not when I use .attr() .但是由于某种原因,它仅在我使用.val() ,而在我使用.attr()时无效。 So the final result is:所以最后的结果是:

$('#quick-view-iot-modal #edit-iot-name').val(currentIoT.appID);

I'm not sure why it is required to add the id of the modal, and I'm not sure why it doesn't work like this:我不确定为什么需要添加模态的 id,我不确定为什么它不能像这样工作:

$('#quick-view-iot-modal #edit-iot-name').attr("value",currentIoT.appID);

But it works!但它有效! If anyone knows why it only works with this combination, please let me know!如果有人知道为什么它只适用于这种组合,请告诉我!

try:尝试:

$('.iot-value[name="iot-name"]').val(currentIoT.appId);

Your selector is pretty weird as well.你的选择器也很奇怪。 why not just refer to the input by name?为什么不只是按名称引用输入?

$('input[name="iot-name"]').val(currentIoT.appId);

In your code here:在您的代码中:

$('.iot-value[name="iot-name"]')

"iot-name" is a string literal, and it will not evaluate to the value of your iot-name variable. "iot-name"是一个字符串文字,它不会计算为您的iot-name变量的值。

I'm guessing that you are looking to use the variable's value in your selector, which would look like this:我猜你想在选择器中使用变量的值,它看起来像这样:

$('.iot-value[name="' + iot-name + '"]')

I have been struggling with this problem for 3 hours.我已经为这个问题苦苦挣扎了 3 个小时。 The realisation that you must reference the object through the modal div is the winner.必须通过模态div引用 object 的认识是赢家。

I was trying to do 2 things:我试图做两件事:

  1. populate the href in an <a> button on my modal在我的模式上的<a>按钮中填充href
  2. set the value of another field using an onChange()使用onChange()设置另一个字段的值

so I end up with:所以我最终得到:

$("#modal-form #linkbtn").attr("href", url);
$('#modal-form #inputfield").val(newVal);

Thanks so much for this hint.非常感谢这个提示。

LISTEN!听! i've got a good solution for all of you guys!我为你们所有人准备了一个很好的解决方案!

$("#exampleModal").modal("show");

    $(function () {
  $("#exampleModal #ticket_id").val("your_value_variable");
});

try this way after you load your modal show it then use DOM ready event then set your value to it input text and amazingly it works!在加载模态显示后尝试这种方式,然后使用 DOM 就绪事件,然后将您的值设置为它输入文本,令人惊讶的是它有效! this is the easiest and simplest way from me这是我最简单的方法

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

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