【问题标题】:jQuery try to set TextBoxFor values based on unchecking CheckBoxFor in MVC3jQuery 尝试基于在 MVC3 中取消选中 CheckBoxFor 来设置 TextBoxFor 值
【发布时间】:2012-01-18 07:51:09
【问题描述】:

我有一个从数据库中填充 checkboxfor 和 textboxfor 的视图。根据模型中有多少条记录,它们有多个。这一切都很好。但现在我想要一个脚本,如果他们取消选中相应的文本框,它将空白。脚本正在运行,但似乎找不到文本框 - 我已经尝试了这 7 种方法到星期天,当它到达将值设置为空字符串的行时,它只是继续前进,没有任何反应。当我查看源代码时,变量中的内容是我试图操作的文本框的 id。我已经用这个把头发拉了2天了。感谢您提供任何帮助。

这是脚本:

<script type="text/javascript">
    $(document).ready(function () {
        $('.chkbox').live('click', function () {
            if ( ! $(this).attr('checked')) 
                {
                var myid = "txtbox_" + $(this).attr('id');
                var varempty = '';
                $("#" + myid).val = (varempty);
            }

        });
    });

这里是我创建 checkboxfor/textboxfor 的地方:

@For x = 0 To Model.Count - 1
      Dim i As Integer = x
      @&lt;tr&gt;
      &lt;td class="checkboxcolumn"&gt;
      @Html.HiddenFor(Function(model) model(i).Soft_ID)
      @Html.HiddenFor(Function(model) model(i).ESR_Soft_ID)
      @Html.CheckBoxFor(Function(model) model(i).Soft_Checked, New With {.id = Model(i).Soft_ID.ToString, .class = "chkbox"})
      &lt;/td&gt;
      &lt;td class="checkcolumn"&gt;
      @Html.Label(Model(i).Soft_Title)
      &lt;/td&gt;
      &lt;td class="checkcolumn"&gt;
      @Html.TextBoxFor(Function(model) model(i).License_Key, New With {.size = "50", .id = "txtbox_" + Model(i).Soft_ID.ToString})
      &lt;/td&gt;
      &lt;/tr&gt;
   Next

这是在页面源中呈现的内容,显示 textboxfor 的 id 是“txtbox_x”,这与我在脚本中放在一起的内容相匹配:

<tr>
      <td class="checkboxcolumn">
      <input data-val="true" data-val-number="The field Soft_ID must be a number." data-val-required="The Soft_ID field is required." name="[0].Soft_ID" type="hidden" value="1" />
      <input data-val="true" data-val-number="The field ESR_Soft_ID must be a number." data-val-required="The ESR_Soft_ID field is required." name="[0].ESR_Soft_ID" type="hidden" value="17" />
      <input checked="checked" class="chkbox" data-val="true" data-val-required="The Soft_Checked field is required." id="1" name="[0].Soft_Checked" type="checkbox" value="true" /><input name="[0].Soft_Checked" type="hidden" value="false" />

      </td>
      <td class="checkcolumn">
      <label for="Adobe_Writer">Adobe Writer</label>
      </td>
      <td class="checkcolumn">
      <input id="txtbox_1" name="[0].License_Key" size="50" type="text" value="adobe key" />
      </td>
      </tr>

任何帮助将不胜感激!我已经在墙上撞了两天了。我尝试设置:eq(index),设置 [input id=variable] 和许多其他的东西都没有成功。它在单击复选框时触发脚本,并正确检索复选框的 id 并正确构建关联文本框的名称 - 它似乎没有找到该字段并将 val 设置为空文本。使用 Firebug 进行调试显示它会命中最后一行,然后退出脚本。

我还应该注意,我是 MVC 和 jQuery 的新手,所以如果我做的不正确,我提前道歉。

【问题讨论】:

    标签: jquery asp.net-mvc-3 checkboxfor


    【解决方案1】:

    这是不正确的语法 - $("#" + myid).val = (varempty); 应该是 - $("#" + myid).val(varempty);

    【讨论】:

      猜你喜欢
      • 2023-03-25
      • 2015-04-13
      • 1970-01-01
      • 2018-09-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-28
      • 2016-09-13
      相关资源
      最近更新 更多