【问题标题】:Disable selected options based on the user's decision根据用户的决定禁用选定的选项
【发布时间】:2015-07-03 22:15:20
【问题描述】:

我试图为用户创建一个开放的环境来选择一个特定的选项,同时给他们以任何顺序选择它们的开放机会,同时消除他们选择的选项。到目前为止,我的代码在乱序执行时会阻止我。基本上选择和选项会在一个跨度内弹出。他们选择一个。结果在该范围内交付,并且出现相同的选项,但消除了他们已经选择的选项。

这是 HTML 代码

<div><select id="lies">
<option value="" selected='selected'>Select a lie...</option>
<option value="FBI">We're undercover agents</option>
<option value="super">We're superheroes</option>
<option value="confess">We're the bombers</option>
</select></div>

<span id='FBI'>
<p>"We're undercover students working with the FBI," you say.</p>
<p>"Get outta here before I put you both in cuffs!" he yells.</p>
<p>"Good job, <?php echo $_GET['Name']?>! You almost got us busted.."   </p>
<p>"Come on, Korra, let's try one more! Over there!" you exclaim.</p>
<div><select id="lies">
<option value="" selected='selected'>Select a lie...</option>
<option value="FBI">We're undercover agents</option>
<option value="super">We're superheroes</option>
<option value="confess">We're the bombers</option>
</select></div>
</span>

<span id='super'>
<p>"Let us through, we're bulletproof!" you fearlessly say.</p>
<p>"Are ya now?" asks the cop as he lightly bops you over the head with his fist.</p>
<p>"OW!" you yell.</p>
<p>"Run along and go play somewhere else, stupid kid!"</p>
<p>You and Korra walk away feeling defeated.</p>
<p>"Any other ideas, <?php echo $_GET['Name']?>?" she asks.</p>
<p>"One more try!" you say as you run over to another police officer.</p>
<div><select id="lies">
<option value="" selected='selected'>Select a lie...</option>
<option value="FBI">We're undercover agents</option>
<option value="super">We're superheroes</option>
<option value="confess">We're the bombers</option>
</select></div>
</span>

<span id='confess'>
<p>"It was us!"</p>
<p>"Alright off to jail you go!" says the officer.</p>
You get escorted off to jail. THE END.
</span>

这里是 jQuery

//Keeps the spans containing the respondes hidden until an option is selected for the lietocops page

$('#lies').change(function(){
var contact = $(this).val();

// Hides the list / show list

if ( contact == ""){
 $('#lies').show();
}else{
$('#lies').hide();
}

$("#lies option:selected").attr('disabled','disabled')
    .siblings().removeAttr('disabled');

// lie responses

if( contact == "FBI") {
 $('#FBI').fadeIn();
}else if( contact == "super") {
 $('#super').fadeIn();
}else if( contact == "confess") {
 $('#confess').fadeIn();
 }
});

【问题讨论】:

  • 我不确定你想在这里做什么,你能重新表述一下吗?
  • 用户可以从列表中选择。选项 a、b 或 c。用户选择了一个选项。故事通过执行该特定跨度来继续选择的选项,并在消除他们已经选择的选项的同时获得另一组选项。我希望能够以任何顺序执行代码。
  • 我正在写一部互动小说

标签: jquery html select options


【解决方案1】:

首先,您有多个 ID 的问题,这是不允许的。你需要让它们成为类。

并且也给你的故事情节一个类(前故事情节)。

所以选择一些东西

var $lies = $(".lies");
$lies.change(function(){
$("#firstLie").hide();
var $this = $(this);
var contact = $this.val();
var $currentStoryline = $this.closest(".storyline");
var $selectedOptions = $(".lies").find("option[value='" + contact + "']");
$selectedOptions.prop("disabled", true);    
$lies.val("");
$currentStoryline.fadeOut();
$("#" + contact).fadeIn();

【讨论】:

  • 所以在这种情况下我必须将 ID 更改为类?并添加其余代码?
  • 是的,刚刚经过测试。您可以在此处看到它的实际效果:elveneleven.com/test.html,因此如果有不清楚的地方,您可以从该页面获取所需的一切。
  • 不错!我需要从当前代码中删除任何内容吗?除了将 ID 更改为类。我应该在哪里实现你的代码?
  • 看看我给你的 test.html 链接。您可以去那里查看源代码并获取那里的代码。然后对其进行改进、修改和进一步调整以满足您的需求。
  • 我对故事情节类有点困惑
猜你喜欢
  • 2022-12-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-10-17
  • 2020-07-03
  • 2016-08-31
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多