0

I have a javascript related question. I have a form with multiple comboboxes being generated while loading:

<select id="email_adrs" class="input-medium">
<option>-Ontvanger-</option>
<?php while ($veri = mysql_fetch_array($verwijzer)) { ?>
   <option value="<?php echo $veri['email'];?>"><?php echo $veri['aanvrager']; ?></option>
<?php 
   }
?>
</select>

When the submit button is clicked, the following code is executed:

<input onclick="sendmail('<?php echo $plist['patient_id']; ?>');" class="submit-green" type="submit" value="Versturen"   name="form_submit" />

The sendmail function is defined as:

function sendmail(pid)
{
    var email = document.getElementById('email_adrs').value;
    var xmlhttp1=new XMLHttpRequest();
    xmlhttp1.onreadystatechange=function()
    {
        if(xmlhttp1.readyState==4 && xmlhttp1.status==200)
        {
            MailNotification();
        }
    }
    xmlhttp1.open('GET','html2pdf/examples/email.php?id='+pid+'&email='+email,true);
    xmlhttp1.send();
}

The problem I am facing is that if I restrict myself to the first record generated, the email is sent. If I select any other record and its corresponding combobox, I get the notification that the mail is sent, however while troubleshooting I found out that there is no value being passed for the email address. It appears to me that the code is not able to detect which combobox was selected.

Please help!

Regards,

Babu

4

1 回答 1

0

您应该将输入更改为

<input onclick="sendmail(this, '<?php echo $plist['patient_id']; ?>');" class="submit-green" type="submit" value="Versturen"   name="form_submit" />

和你的功能

function sendmail(clickedElement, pid)
{
    var formElem = clickedElement.parentElement; // (1)
    var selectElem = formElem.getElementsByTagName("select")[0]; // (2)
    var email = selectElem.value;

    /*
      [Ajax call here]
    */
}

.parentElement(1):根据您的布局,您必须申请尽可能多的表格才能到达表格。
(2):如果您有超过 1 个select元素,请使用数组的适当索引。

如果您使用的是 JQuery,那么您可以使用

var email = $(clickedElement).closest("form").find("#email_adrs").val();
于 2012-12-14T21:03:24.707 回答