【问题标题】:Unable to get drop down value无法获取下拉值
【发布时间】:2014-09-06 10:17:04
【问题描述】:

通过使用下面的代码,我试图获取 div html 值。我很难获得 drop selected 值。我得到这样的- select -agreedeny 而只是选择的值。如何仅获取选定的值。对于此示例,agreedeny
CODE

print <<HTML_TOP;

 <!doctype html>
<html>
<head>
$tornadodocumentheader

<script type="text/javascript">

\$(document).ready(function(){

  \$('#form').submit(function (e) {
    e.preventDefault();
var \$form = \$('#form').data('Zebra_Form');
if (\$form.validate()) {
    var contents = escape(\$('#myDiv').html());
    //var contents = \$('#myDiv').html();
    //var contents = escape(\$('#myDiv').HTML());
    alert(contents);
    contents = contents.replace(/%3D/g,"equaltoequaltoequalto");

      \$.ajax({
         cache: false,
         url: 'pdfcontent.cgi?content='+contents+' ',
         success: function(msg, ts, xh){
               alert("success");
         },
         processData: false,
         error: function(X, textStatus, error){
             alert("Error saving... please try again now.")
         }
     });
          }
     });
});
</script>
</head>
<body>
<br>

<form name="form" id="form" action="#" method="post"  class="Zebra_Form">
<div id="myDiv">
<h4>Project Name: $project</h4>
<h4>Job No: $workorderno</h4>
<h4>Processed By: $f_name</h4>
<h4>Processed On: $printdate</h4>

HTML_TOP

my $diff = XML::SemanticDiff->new(keepdata => 1,keeplinenums => 1);
$missingelementcounter=0;
$roguecounter =0;
$mismatchcounter =0;

foreach my $change ($diff->compare($f1, $f2)) {  
if ($change->{message} ne "")
{
$processnos =1;
$message = $change->{message};
          #print Dumper($change);
          $new_value = $change->{new_value};
          $old_value = $change->{old_value};
          $context = $change->{context};
          my ($value)=$message =~ m/Character differences in element ('[^>]*')/igs;
            @arraymismatch[$mismatchcounter] = "$value;$new_value;$old_value;$context";
            $mismatchcounter++;
          }
}
$totalmismatch = scalar @arraymismatch;
         print "Total Mismatch Elements: $totalmismatch\n"; 
          print <<HTML_TOP;

<table align='center' border="1">
<thead>
<th>Element</th>
<th>Artwork XML</th>
<th>Tornado XML</th>
<th>Changes is accepted?</th>
<thead> <p></p>
<tbody>

HTML_TOP
for ($i=0;$i<(scalar @arraymismatch);$i++)
          {
          $temp = $arraymismatch[$i];
            my @words = split /;/,, $temp;          
            $contextWords = @words[3];
            my @wordsElement = split '[/.-]', $contextWords;
            $missingElementposition = $wordsElement[3];
            $find = "CopyElement[";
            my $replace = "";
            $find = quotemeta $find;            
            $missingElementposition =~ s/$find/$replace/g;          
            $find = "]";
            my $replace = "";
            $find = quotemeta $find; 
            $missingElementposition =~ s/$find/$replace/g;          
            XML::Twig->new( twig_handlers =>  # in the handler $_ is the department element
                      { CopyElement => sub {                      
                      my $value =  $_->att( 'CopyElementType');                   
                      @ElementNameArray[$counter] = $value;
                      $counter++;                     
                      } 
                      }
                      )
             ->parsefile( $f2);          
             XML::Twig->new( twig_handlers =>  # in the handler $_ is the department element
                      { CopyElement => sub { 
                      my $value =  $_->att( 'CopyElementType');                   
                      @ElementNameArray1[$counter] = $value;
                      $counter++;                     
                      } 
                      }
                      )
             ->parsefile( $f1);          
                 print <<HTML_TOP; 
<tr class='row' align='left'><td>$ElementNameArray[$missingElementposition-1]</td><td>$words[1]</td><td>$words[2]</td><td><select name='acceptance$processnos' id='acceptance$processnos' class='control other'><option value=''>- select -</option><option value='agree'>agree</option><option value='deny'>deny</option></select></td></tr>
</div>
HTML_TOP

【问题讨论】:

    标签: jquery html ajax perl


    【解决方案1】:

    要获取 HTML 表单元素的选定值,请使用 val 方法。只需读取textcontents 将返回浏览器呈现的文本——所有选项连接的文本。

    【讨论】:

    • 如果您在jsfiddle.net 演示中创建一个简单的示例,我可以根据需要进行更新。否则看看这个快速方法比较:jsfiddle.net/c6ag2
    • 我需要获取 div html,而不仅仅是下拉值。我的 div 包含表格、标题和下拉列表。给出的示例只关注下拉菜单。我如何获得 DIV 中的所有 htnl
    猜你喜欢
    • 1970-01-01
    • 2020-08-08
    • 1970-01-01
    • 1970-01-01
    • 2021-03-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多