JavaScript的;复选框功能

时间:2017-02-02 04:20:08

标签: javascript

下面是我的代码,我需要使用函数中复选框的值,然后在文本框中显示它。我试过更改功能,但我无法得到它。格式化和一切都很好我认为它必须是jscript的语法。帮助



    function getTotal(redDress, blueDress, greenDress ) {
        var redDress= 15.99 (document.getElementById("redDress").checked);
        var blueDress= 19.99 (document.getElementById("blueDress").checked);
        var greenDress= 20.99(document.getElementById("greenDress").checked);
        var subtotal= greenDress + blueDress + redDress;
        var taxAmount = .07;
        var tax = subtotal * taxAmount;
        var total = subtotal + tax;

        if((redDress=false)&&(blueDress=false)&&(greenDress=false)){
            alert("Please select a dress");
            return false && true;
        }
    }

<form method ="get" onsubmit="return is_checked()">
        <p>Please select one or more dresses then hit submit:</p>
        <input type="checkbox" value="15.99" id="redDress">Red Dress ($15.99)   <br>
        <input type="checkbox" value="19.99" id="blueDress">Blue Dress ($19.99)<br>
        <input type="checkbox" value="20.99" id="greenDress">Green Dress ($20.99)    <br>
        <input type="submit" value="submit" onclick="getTotal()"><br>
    <br>
        <br>
        Subtotal:<input type="text" name="subtotal" id="subTotal">
        <br>
        <br>
        Total:<input type="text" name="total" id="total" ><br>
        <br>
        Tax:<input type="text" name="tax" id="tax" >
        <br><br>
        This purchase was made on:
    </form>
&#13;
&#13;
&#13;

5 个答案:

答案 0 :(得分:0)

这个var redDress= 15.99 (document.getElementById("redDress").checked);会抛出错误,说15.99不是函数,因为它是函数调用的语法(foo (something);)。我认为您正在寻找的是三元运算符,如下所示:

var redDress= (document.getElementById("redDress").checked)? 15.99: 0;

与(或多或少)相同:

var redDress = 0;
if(document.getElementById("redDress").checked)
    redDress = 15.99;

(如果用户选中redDress复选框,则redDress的值将为15.99,否则将为默认0

注1:blueDressgreenDress以及whateverOtherDress...执行相同操作。

注2:检查用户是否选择了某些内容,只需检查总subtotal是否等于0,如下所示:

if(subtotal == 0){
    alert("Please select a dress");
    return false; // return false , not false && trus which is the same as false
}

注3:如果在函数内重新定义它们,则不需要这些参数。所以这个:

function getTotal(redDress, blueDress, greenDress ) {
    //...

应该只是:

function getTotal() {
    // ...

答案 1 :(得分:0)

最快的解决方法是使用三元作为价格变量。

var redDress = document.getElementById("redDress").checked ? 15.99 : 0;
//if redDress is checked, redDress price = 15.99, else 0
var blueDress= document.getElementById("blueDress").checked ? 19.99 : 0;
//if blueDress is checked, blueDress price = 19.99, else 0
var greenDress= document.getElementById("greenDress").checked ? 20.99 : 0;
//if greenDress is checked, greenDress price = 20.99, else 0

答案 2 :(得分:0)

这可能是一种更好的方法:

https://jsfiddle.net/e4085pap/

这是在原始代码的上下文中:

<html>
<head>
<script>
window.onload = function() {
    //listen for form submission event
    document.querySelector('form').addEventListener('submit', function(event) {
        //prevent default so form doesn't actually submit and reload the page
        event.preventDefault()
        //select all of the checkboxes and loop over them so we can determine which one is checked
        document.querySelectorAll('input[type="checkbox"]').forEach(function(input) {
            // if the input is checked then pass the cost of the dress to a function that will display the values
            if (input.checked) {
                displayValues(Number(input.value))
            }
        })
    }) 

    function displayValues(value) {
        var tax = 0.07;
        document.querySelector('input[name="subtotal"]').value = value
        document.querySelector('input[name="total"]').value = value + value * tax
        document.querySelector('input[name="tax"]').value = tax 
    }   
}
</script>
</head>
<body>

<form method ="get" onsubmit="return">
<p>Please select one or more dresses then hit submit:</p>
<input type="checkbox" value="15.99" id="redDress">Red Dress ($15.99)<br>
<input type="checkbox" value="19.99" id="blueDress">Blue Dress ($19.99)<br>
<input type="checkbox" value="20.99" id="greenDress">Green Dress ($20.99)    <br>
<input type="submit" value="submit"><br>
<br>
<br>
Subtotal:<input type="text" name="subtotal" id="subTotal">
<br>
<br>
Total:<input type="text" name="total" id="total" ><br>
<br>
Tax:<input type="text" name="tax" id="tax" >
<br><br>
This purchase was made on:

</form>
</body>
</html>

答案 3 :(得分:0)

对代码的此更新将获取变量值并在文本框中显示结果,原始请求也是如此:

<html>
   <head>
      <script type="text/javascript">


function getTotal(redDress, blueDress, greenDress ) {

         var redDress= document.getElementById("redDress").checked ? 15.99 :0;
         var blueDress= document.getElementById("blueDress").checked ? 19.99 :0;
         var greenDress= document.getElementById("greenDress").checked ? 20.99 :0;
         var subtotal= greenDress + blueDress + redDress;
         var taxAmount = .07;
         var tax = subtotal * taxAmount;
         var total = subtotal + tax;

         if((redDress==false)&&(blueDress==false)&&(greenDress==false)){alert("Please select a dress")}

         document.getElementById('total').value = total;
         document.getElementById('subTotal').value = subtotal;
         document.getElementById('tax').value = tax;

     }

      </script>
   </head>
   <body>
      <form method ="get">
         <p>Please select one or more dresses then hit submit:</p>
         <input type="checkbox" value="15.99" id="redDress">Red Dress ($15.99)<br>
         <input type="checkbox" value="19.99" id="blueDress">Blue Dress ($19.99)<br>
         <input type="checkbox" value="20.99" id="greenDress">Green Dress ($20.99)    <br>
         <input type="button" value="Display" onclick="getTotal()"><br>
         <br>
         <br>
         Subtotal:<input type="text" name="subtotal" id="subTotal">
         <br>
         <br>
         Total:<input type="text" name="total" id="total"><br>
         <br>
         Tax:<input type="text" name="tax" id="tax" >
         <br><br>
         This purchase was made on:
      </form>
   </body>
</html>

答案 4 :(得分:0)

这是解决方案。现在您可以从javascript获取复选框值。 代码示例:

/**  
 *
 * Register your CPT. This is not complete code
 *
 * If you want to have a child page of a CPT single make sure that the $args includes:
 *
 * 'capability_type' => 'page',
 * 'hierarchical'    => true,  
 * 'supports'        => array( 'page-attributes' ),
 */
function myprefix_register_mycpts() {

    /**
     * Post Type: MyCpt
     */

    $labels = array(
        'name' => __( 'MyCpts', '' ),
        'singular_name' => __( 'MyCpt', '' ),
    );

    $args = array(

        //... indicates others

        'capability_type' => 'page',
        'hierarchical'    => true, 
        'supports'        => array( 'page-attributes' ),

    );

    register_post_type( 'mycpt', $args );

}
add_action( 'init', 'myprefix_register_mycpts' );


/**  
 * 
 * Shortcode
 * Get the Child Links
 * Use CSS to style the .child-link-pages class and its children
 *
 * usage: [get-child-page-links]
 * By: me
 *
 */
function yourprefix_get_child_pages() { 

    global $post;

    $args = array(
        'post_type'           => 'any',
        'posts_per_page'      => -1,
        'post_parent'         => $post->ID,
        'order'               => 'ASC',
        'orderby'             => 'menu_order',
        'ignore_sticky_posts' => 1,
    );

    $parent = new WP_Query( $args );
    $output = '';

    if ( $parent->have_posts() ) : 

        $output .= '<ul class="child-page-links">';

        while ( $parent->have_posts() ) : $parent->the_post(); 

            $output .= '<li>';
            $output .= '<a class="button" href="' . get_the_permalink() . '">';
            $output .= get_the_title();
            $output .= '</a>';
            $output .= '</li>';

        endwhile;

        $output .= '</ul>';

    endif; 
    wp_reset_postdata();

    //shortcodes return not echo!
    return $output;

}
add_shortcode('get-child-page-links', 'yourprefix_get_child_pages' );



/**  
 *
 * Set the child template for a CPT
 * https://codex.wordpress.org/Plugin_API/Filter_Reference/template_include
 * http://wordpress.stackexchange.com/a/145188/64742
 * by Toscho -- awesome!
 *
 * change mycpt to yours
 *
 */
add_filter( 'template_include', function( $template ) {

    if ( ! is_singular() )
        return $template; // not single

    if ( 'mycpt' !== get_post_type() )
        return $template; // wrong post type

    if ( 0 === get_post()->post_parent )
        return $template; // not a child

    return locate_template( 'single-child-mycpt.php' );
} );