2011-05-09 74 views
-1

我有麻烦。我的php文件没有得到正确的结果

我编写HTML和jQuery并设置相同的名称名称属性<select>标记和<input>标记。

<select name="questionBoxOne" id="questionBoxOne"> 
    <option selected="selected" value="">Choose a question</option> 
    <option value="">---------------</option> 
    <option value="custom">Create your own question</option> 
    <option value="">---------------</option> 
    <option value="My mother's maiden name?">My mother's maiden name?</option> 
    <option value="The name of my first pet?">The name of my first pet?</option> 
    <option value="My father's middle name?">My father's middle name?</option> 
</select> 

我用这个区域显示输入框,如果用户选择自定义问题选项。

<div id="newQuestion"> 
    <input type="text" value="" id="questionBoxOne" name="questionBoxOne" /> 
    <a id="cancel">select question</a> 
</div> 

而我使用jQuery代码。

<script language="javascript"> 
     $(function() { 
      $('#newQuestion').hide(); 

      $('#questionBoxOne').change(function() { 
       if ($(this).val() === 'custom') { 
        $('#newQuestion').show(); 
        $('#questionBoxOne').hide(); 
       } 
      }); 

      $('#cancel').click(function() { 
       $('#questionBoxOne').show(); 
       $('#newQuestion').hide(); 
      }); 
</script> 

,你可以在我前面的代码看到我发送相同我的下一个finish.php文件,但是当我试图这样它没有显示<select>选项的问题,但这个代码显示燃烧问题。

请帮我解决这个问题。

+0

OK,我认为你隐藏的选择项目时,你想要做的是禁用它?关于你想如何表现的更多解释。还有什么“正如你在我以前的代码中看到的,我正在向我的下一个finish.php文件发送相同的名称”,意味着更早的问题链接也许? – 2011-05-09 17:43:49

+0

@Laurence伯克 - 我想当有人选择自定义问题选项,输入标签变得活跃,用户能够提交他/她的问题。但是当用户从下拉菜单中选择任何问题时。该变量通过finish.php ..但我的问题是,当我选择自定义选项输入框显示并传递名称=“questionBoxOne”到finish.php,但是当我从下拉菜单中选择问题,然后名称=“questionBoxOne”传递给finish.php为空 – Muzammil 2011-05-09 17:47:45

回答

1

好吧,我想我明白了。要禁用的时候你不使用所以这里的名字是一些代码

<script language="javascript"> 
    $(function() { 
     $('#newQuestion').hide(); 
     $('#newQuestion input').removeAttr('name'); 
     $('#questionBoxOne').change(function() { 
      if ($(this).val() === 'custom') { 
       $('#newQuestion').show(); 
       $('#newQuestion input').attr('name','questionBoxOne'); 
       $('#questionBoxOne').hide(); 
       $('#questionBoxOne').removeAttr('name'); 
      } 
     }); 

     $('#cancel').click(function() { 
      $('#questionBoxOne').show(); 
      $('#questionBoxOne').attr('name','questionBoxOne'); 
      $('#newQuestion input').removeAttr('name'); 
      $('#newQuestion').hide(); 
     }); 
</script> 

应该工作

但不是从#newQuestion第一removeattr您应该删除的name属性在HTML 是的,也是非常重要的获得ID的ID输入NEWQUESTION!

然而,在你应该处理这个问题的方法是

<form id="submit" action="finish.php" method="post"> 
    <select id="questionSelect" name="questionSelect"> 
     <option selected="selected" value="">Choose a question</option> 
     <option value="">---------------</option> 
     <option value="custom">Create your own question</option> 
     <option value="">---------------</option> 
     <option value="My mother's maiden name?">My mother's maiden name?</option> 
     <option value="The name of my first pet?">The name of my first pet?</option> 
     <option value="My father's middle name?">My father's middle name?</option> 
    </select> 
    <input type="text" value="" id="custQuestion" name="custQuestion" /> 
    <a id="cancel">select question</a> 

则保留原来的jQuery功能与IDS修改为相应的像这样

<script language="javascript"> 
     $(function() { 
      $('#custQuestion').hide(); 

      $('#questionSelect').change(function() { 
       if ($(this).val() === 'custom') { 
        $('#custQuestion').show(); 
        $('#questionSelect').hide(); 
       } 
      }); 

      $('#cancel').click(function() { 
       $('#questionSelect').show(); 
       $('#custQuestion').attr('value','').hide(); 
      }); 
</script> 

现在的finish.php您可以查找返回的名称并使用该名称,具体取决于自定义问题是否为空。

像这样

if(!empty($_POST['custQuestion')) 
    $question = $_POST['custQuestion']; 
else 
    $question = $_POST['questionSelect']; 

现在如果不帮助你需要做更多的研究

+0

它不会工作,但你有正确的想法。不是你的错,他在两个元素上有'questionBoxOne' id属性(这是错误的并且会导致错误),所以你应该使用'$('#newQuestion input')'选择文本输入,并且OP应该修复他错误。另外,您需要将html中禁用的文本输入设置为默认值。换句话说,'#newQuestion'是div的id,而不是输入。 – 2011-05-09 17:57:45

+0

'#newQuestion'是一个div,而不是输入。 – 2011-05-09 17:59:56

+0

这还是不对的。顺便说一句好的头像:) – 2011-05-09 18:01:45

相关问题