【问题标题】:JavaScript function for radio button input fields单选按钮输入字段的 JavaScript 函数
【发布时间】:2015-07-16 08:05:38
【问题描述】:

目前我制作了两个单选按钮,一个显示输入字段,另一个不显示。单击另一个单选按钮时,我将如何修改我的 java 脚本函数以显示不同的输入字段。

<tr>
  <td class="odd" width="20%" nowrap>
    <bean:message key="editvarref.header.name"/>
  </td>
  <td class="odd" width="80%">
    <div>
      <html:radio styleId="users_name_type_0" property="user_type" value="Show" onclick="getResults(this)" />
     <label for="user_name_type_0"><bean:message key="input.use.name"/> </label>
    </div>
    <div style="padding-top:5px; padding-bottom:5px;">
    <html:radio styleId="email_name_type_1" property="user_type" value="Nothing" onclick="getResults(this)" />
    <label for="email_name_type_1"><bean:message key="input.use.email"/></label>
    </div>

    <div class="text">
      <p>First Name:
        <input type="text" name="text1" id="text1" maxlength="30">
      </p>
    </div>
    <div class="text">
      <p>Last Name:
        <input type="text" name="text1" id="text1" maxlength="30">
      </p>
    </div>
  </td>
</tr>  

我使用的js函数是getResults():

function getResults(elem) {
        elem.checked && elem.value == "Show" ? $(".text").show() : $(".text").hide();
       };

我尝试使用 if 语句,但出现错误,当我使用 getElementById 尝试时,只有一个按钮起作用。我将如何修改 JavaScript 函数以显示另一个 div

    <div class="email">
      <p>Email:
        <input type="text" name="email1" id="email1" maxlength="30">
      </p>
    </div>

我还必须在重置函数中添加控制语句吗?

【问题讨论】:

    标签: javascript html jsp struts-1


    【解决方案1】:

    (您需要比“if 语句”更具体,以便人们了解您已经尝试过的内容。)但是尝试将您的 onclick 函数更改为:

    function getResults(elem){
      if (elem.value == "Show") {
        $(".text").show();
        $(".email").hide();
      } else {
        $(".email").show();
        $(".text").hide();        
      }
    }
    

    【讨论】:

      猜你喜欢
      • 2011-05-18
      • 1970-01-01
      • 2020-12-03
      • 1970-01-01
      • 2012-08-13
      • 2013-02-04
      • 1970-01-01
      • 1970-01-01
      • 2012-05-12
      相关资源
      最近更新 更多