2015-03-31 169 views
0

Hello Im新的Html/Java(脚本/查询)/ css,大约半年。
我有几个问题。
现在我试图做一个功能/代码,这样做:我有2个单选按钮。
点击第1个单选按钮选择第1个选项,并删除其他选项。
,同时点击第二个单选按钮可打开其他选项,并删除第一个将自己专用于第一个单选按钮的选项。Jquery代码,单击单选按钮选择一个选项

<form enctype="multipart/form-data"> 
    <fieldset> 
     <legend>מפרט טכני של הסמארטפון:</legend> 
     <label for="memory">זיכרון פנימי:</label> 
     <input id="less1" type="radio" value="less" name="memory" onclick="(myFun())" /> פחות מ 1GB 
     <input id="more1" type="radio" value="more" name="memory" onclick="(myFun1())" />מעל 1GB 
     <br /> 
     <label for="extra">אפשרות להרחבת זיכרון:</label> 
     <br /> 
     <select size="5" id="sel"> 
      <option name="extra" value="no1" id="no">לא</option> 
      <option name="extra" value="yes8">כן,8GB</option> 
      <option name="extra" value="yes16">כן,16GB</option> 
      <option name="extra" value="yes32">כן,32GB</option> 
      <option name="extra" value="yes64">כן,64GB</option> 
     </select> 
     </fieldset> 
</form> 

 function myFun() { 
     $("#sel").prop('size', "1") 
     select('option', $("no")).select('option:not(:selected)').remove()   
    } 
    function myFun1() { 
     $("#sel").prop('size',"4") 
    } 

我这里有checke另一个话题,它helpd我有点(jQuery on change only shows the selected option, remove/disable rest of them

+0

我想了解你想要做什么。因此,如果您单击要设置为第一个选项的第一个单选按钮并隐藏其他单选按钮,并且如果单击第二个单选按钮,则显示隐藏的选项并隐藏第一个选项。那是对的吗? – abaracedo 2015-03-31 18:45:46

+0

是的。那就是我想要的 – 2015-03-31 19:04:48

回答

0

这就是代码,你想要做什么。我删除了select()事件,因为当使用鼠标选择文本时会触发该事件。

最好隐藏/显示元素,而不是删除它们。

function myFun() { 

    // 1. Find the first option and set as selected 
    $("#sel").find('option').first().prop("selected",true) 
    // 2. Find its not selected siblings and hide it 
    .siblings('option:not(:selected)').hide(); 

    // 3. The first radio button is selected, so show the first option 
    $("#no").show(); 

} 

function myFun1() { 

    // 1. Find the last option, set as selected and show it 
    $("#sel").find('option').last().prop("selected",true).show() 
    // 2. Find its siblings and show them 
    .siblings('option:not(:selected)').show(); 

    // 3. Hide the first option 
    $("#no").hide(); 

} 
+0

你可以添加评论吗? (// * text * //)向我解释你添加的其他文本在做什么? 我知道它做什么,但解释会帮助我对未来的东西 – 2015-03-31 19:11:28

+0

评论添加。 – abaracedo 2015-03-31 19:15:39

+0

非常感谢mate.//编辑即将来临// 我还有一个问题。如果我删除$(“#sel”)。find('option')。last()。prop(“selected”,true).show(),那么它显示该选项而不选择最后一个。而且当我按下第一个单选按钮,然后切换到第二个,它只显示滚动条。为什么? 以及我如何能做到这一点,它会显示4选项,而不会选择最后一个? – 2015-03-31 19:18:52