【问题标题】:hide/show multiple buttons with Select menu onChange使用选择菜单 onChange 隐藏/显示多个按钮
【发布时间】:2014-03-22 17:25:38
【问题描述】:

我不确定是否有人可以帮助我解决这个问题,但我目前有一个带有 onchange 的 ,它在切换时显示 2 种不同的形式。问题是我在表单之外还有一个更新按钮,并且在表单更改时也尝试更改按钮。

下面是我的代码。谁能帮我解决这个问题。

JavaScript:

隐藏/显示

function changelocation(val) {
    var id = val;
    //alert(id);
    if (id == '1') {
        $('#tr_1').css('display', 'table-row');
        $('#tr_2').css('display', 'none');
    }
    if (id == '2') {
        $('#tr_2').css('display', 'table-row');
        $('#tr_1').css('display', 'none');
    }
}

在表单外显示保存按钮

$(文档).ready(函数 () { $("#location1_update").click(function () { $("#location1").submit(); }); });

$(document).ready(function () {
    $("#location2_update").click(function () {
        $("#location2").submit();
    });
});

按钮

<input class="submit-button" type="submit" name="location1_update" value="Update">
<input class="submit-button" type="submit" name="location2_update" value="Update">

- 使用 smarty note - 这很好用。

<select class="selmenu-wo" name="company_locations" onChange="changelocation(this.value)">
    <option value="1" selected="selected">{section name=r loop=$location1}{$location1[r].LOCATION}
&nbsp;&nbsp;{/section}</option>
    <option value="2">{section name=q loop=$location2}{$location2[q].LOCATION}&nbsp;&nbsp;{/section}
    </option>
</select> 

表格。

<form action="" id="location1" name="location1" method="POST">
<table  cellpadding="0" id="tr_1"  cellspacing="0">
<tr> <td><b>Name</b></td>

<input name="test" type="text" value="Test data">

</td></tr>
</table>
</form>

<form action="" id="location2" name="location2" method="POST">
<table  cellpadding="0" id="tr_2" style="display:none" cellspacing="0">
<tr> <td><b>Name</b></td>

<input name="test2" type="text" value="Test data">

</td></tr>
</table>
</form>

表单切换正常,但我希望它在切换时也显示按钮。 如果我为表单和按钮分配相同的 id,则只有按钮切换。

【问题讨论】:

  • 为什么不在相应的表单中包含按钮?
  • 您是否希望在选择的值为1时显示location1_update并隐藏位置2_update? span>
  • 我不想将它包含在表单中,因为它看起来不太好,当我显示按钮时我还有另一个表。更像是在页面的标题处。
  • @user3401335 是的,我想在选择表单 1 时隐藏位置 2 按钮,并在选择表单 2 时隐藏位置 1
  • 我不太了解...所以您有一个具有 2 个或多个值的“选择菜单”...当您选择其中一个值时,它应该出现一个或另一个表单,根据在“选择菜单”中选择的值...您能否更具体地了解按钮?

标签: jquery html smarty


【解决方案1】:

使用

$("#location1_update").hide()

$("#location1_update").show()

用于隐藏和显示按钮。 更改此html whit id 而不是名称

<input class="submit-button" type="submit" id="location1_update" value="Update">
<input class="submit-button" type="submit" id="location2_update" value="Update">

那么js可以是

if (id == '1') {
    $('#tr_1').css('display', 'table-row');
    $('#tr_2').css('display', 'none');
    $("#location1_update").show();
    $("#location2_update").hide();

}
if (id == '2') {
    $('#tr_2').css('display', 'table-row');
    $('#tr_1').css('display', 'none');
    $("#location2_update").show();
    $("#location1_update").hide();
}

【讨论】:

  • 完美!。我是个白痴,应该自己解决这个问题,但感谢您的帮助... :)
【解决方案2】:

为按钮和表单添加相同的类例如:

<input class="submit-button location1" type="submit" name="location1_update" value="Update">
<input class="submit-button location2" type="submit" name="location2_update" value="Update">

<form action="" id="location1" class="location1" name="location1" method="POST">
<form action="" id="location2" class="location2" name="location2" method="POST">

然后像这样显示/隐藏它们:

function changelocation(val) {
    var id = val;
    if (id == '1') {
        $('.location1').show();
        $('.location2').hide();
    }
    if (id == '2') {
        $('.location2').show();
        $('.location1').hide();
    }
}

【讨论】:

  • 感谢您的帮助,也可以,如果值为 1,则只需添加样式以在第二个按钮上不显示
猜你喜欢
  • 2014-06-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多